package com.luo.shop.service.impl.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.luo.shop.dao.CommentDao;
import com.luo.shop.dao.GoodsBoDao;
import com.luo.shop.dao.GoodsDao;
import com.luo.shop.pojo.Goods;
import com.luo.shop.pojo.GoodsBo;
import com.luo.shop.pojo.Page;
import com.luo.shop.pojo.Goods;
import com.luo.shop.service.admin.GoodsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class GoodsAdminServiceImpl implements GoodsAdminService {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsBoDao goodsBoDao;

    public Page<Goods> selectAll(String title, Long cid, Integer pageNum,Boolean isputaway) {
        Example example = new Example(Goods.class);
        Example.Criteria criteria = example.createCriteria();
        //模糊查询字段
        if (StringUtil.isNotEmpty(title)) {
            criteria.andLike("title", "%" + title + "%");
        }
        if (StringUtil.isNotEmpty(cid + "")) {
            criteria.andEqualTo("cid", cid);
        }
        if (StringUtil.isNotEmpty(isputaway + "")) {
            criteria.andEqualTo("isputaway", isputaway);
        }
        //开启分页
        PageHelper.startPage(pageNum, 5);
        //查询结果集
        List<Goods> Goodss = goodsDao.selectByExample(example);
        //封装结果集对象
        PageInfo<Goods> pageInfo = new PageInfo<Goods>(Goodss);
        Page<Goods> page = new Page<Goods>();
        page.setItems(pageInfo.getList());
        page.setPageNum(pageInfo.getPageNum());
        page.setPages(pageInfo.getPages());
        //展示分页10条页码
        // 1.一共展示10个页码，能够达到前5后4的效果
        // 2.如果前边不够5个，后边补齐10个
        // 3.如果后边不足4个，前边补齐10个
        Integer home = 0;  //开始
        Integer end = 0;   //结尾
        if (pageInfo.getPages() < 10) {  //如果总页数小于10,就把home设置为1,end设置为总页数
            home = 1;
            end = pageInfo.getPages();
        } else {        //如果总页数大于10,就前5后4
            home = pageInfo.getPageNum() - 5;
            end = pageInfo.getPageNum() + 4;
            if (home < 1) {   //如果home小于1,就home设置为1，end设置为10
                home = 1;
                end = 10;
            }
            if (end > pageInfo.getPages()) {  //如果end大于总页数,end设置为总页数，home设置为end-9，前边补充
                end = pageInfo.getPages();
                home = end - 9;
            }
        }
        page.setHome(home);
        page.setEnd(end);
        return page;
    }

    @Transactional
    public Integer delete(Long id) {
        Goods goods  = new Goods();
        goods.setId(id);
        goods.setIsputaway(false);
        return goodsDao.updateByPrimaryKeySelective(goods);
    }

    @Transactional
    public Integer update(Goods goods) {
        GoodsBo goodsBo = new GoodsBo();
        goodsBo.setGid(goods.getId());
        goodsBoDao.delete(goodsBo);
        int i = goodsDao.updateByPrimaryKeySelective(goods);
        if (i > 0) {
            for (int j = 0; j <goods.getType().size() ; j++) {
                 goodsBo = new GoodsBo();
                goodsBo.setGid(goods.getId());
                goodsBo.setImage(goods.getImageBo().get(j));
                goodsBo.setType(goods.getType().get(j));
                goodsBoDao.insertSelective(goodsBo);
            }
        }

        return i;
    }
    @Transactional
    public Integer save(Goods goods) {
        goods.setIsputaway(true);
        goods.setCreatetime(new Date());
        int i = goodsDao.insertSelective(goods);
        if (i > 0) {
            for (int j = 0; j <goods.getType().size() ; j++) {
                GoodsBo goodsBo = new GoodsBo();
                goodsBo.setGid(goods.getId());
                goodsBo.setImage(goods.getImageBo().get(j));
                goodsBo.setType(goods.getType().get(j));
                goodsBoDao.insertSelective(goodsBo);
            }
        }
        return i;
    }

    public Goods querbyById(Long id) {
        Goods goods = goodsDao.selectByPrimaryKey(id);
        GoodsBo goodsBo = new GoodsBo();
        goodsBo.setGid(goods.getId());
        goods.setGoodsBos(goodsBoDao.select(goodsBo));
        return goods;
    }

    public Integer selectCount() {
        return goodsDao.selectCount(null);
    }
}
